package per.joywong.blogauth.response.token;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;
import java.util.Set;

@JsonSerialize
@JsonDeserialize
@JsonIgnoreProperties({
        "value",
        "scope",
        "expiration",
        "expired",
        "additionalInformation",
        "refreshToken",
        "tokenType",
        "expiresIn"
})
public class BlogOauth2AccessToken implements Serializable, OAuth2AccessToken {

    private static final long serialVersionUID = 914967629530462926L;

    /**
     * Create an access token from the value provided.
     */
    public BlogOauth2AccessToken(String value) {
        this.token = new DefaultOAuth2AccessToken(value);
    }

    /**
     * Private constructor for JPA and other serialization tools.
     */
    @SuppressWarnings("unused")
    private BlogOauth2AccessToken() {
        this((String) null);
    }

    /**
     * Copy constructor for access token.
     *
     * @param accessToken
     */
    public BlogOauth2AccessToken(OAuth2AccessToken accessToken) {
        this(accessToken.getValue());
        this.token.setAdditionalInformation(accessToken.getAdditionalInformation());
        this.token.setRefreshToken(accessToken.getRefreshToken());
        this.token.setExpiration(accessToken.getExpiration());
        this.token.setScope(accessToken.getScope());
        this.token.setTokenType(accessToken.getTokenType());
    }

    private DefaultOAuth2AccessToken token;

    public DefaultOAuth2AccessToken getToken() {
        return token;
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return this.token.getAdditionalInformation();
    }

    @Override
    public Set<String> getScope() {
        return this.token.getScope();
    }

    @Override
    public OAuth2RefreshToken getRefreshToken() {
        return this.token.getRefreshToken();
    }

    @Override
    public String getTokenType() {
        return this.token.getTokenType();
    }

    @Override
    public boolean isExpired() {
        return this.token.isExpired();
    }

    @Override
    public Date getExpiration() {
        return this.token.getExpiration();
    }

    @Override
    public int getExpiresIn() {
        return this.token.getExpiresIn();
    }

    @Override
    public String getValue() {
        return this.token.getValue();
    }
}
